package org.leon.mybatis.test;

import org.apache.ibatis.session.SqlSession;
import org.leon.mybatis.domain.User;
import org.leon.mybatis.factory.MySqlSessionFactory;
import org.leon.mybatis.mapper.UserAnnotationMapper;

import static java.lang.System.out;

/**
 * Created by HuGuodong on 2017/5/2.
 * For Mybatis Annotation Usage
 */
public class AnnotationSqlSessonFactory {

    /**
     * MyBaits Annotation Usage
     *
     */
    public static void Anno_selectUserById(){
        SqlSession session = MySqlSessionFactory.getSqlSession();
        UserAnnotationMapper pm = session.getMapper(UserAnnotationMapper.class);
        User u = pm.selectUserById(5);
        out.println(u);
        MySqlSessionFactory.releaseSqlSession(session);
    }

    /**
     * test insert user
     */
    public static void Anno_insertUser(){
        SqlSession session = MySqlSessionFactory.getSqlSession();
        UserAnnotationMapper pm = session.getMapper(UserAnnotationMapper.class);
        User u = new User("wangtiaw","F",16);
        pm.saveUser(u);
        MySqlSessionFactory.releaseSqlSession(session);
    }


    /**
     * test modify user
     */
    public static void Anno_modifyUser() {
        SqlSession session = MySqlSessionFactory.getSqlSession();
        UserAnnotationMapper pm = session.getMapper(UserAnnotationMapper.class);
        User u = pm.selectUserById(7);
        u.setSex("M");
        u.setAge(30);
        pm.modifyUser(u);
        MySqlSessionFactory.releaseSqlSession(session);
    }

    /**
     * test delete user
     */
    public static void Anno_deleteUser() {
        SqlSession session = MySqlSessionFactory.getSqlSession();
        UserAnnotationMapper pm = session.getMapper(UserAnnotationMapper.class);
        pm.removeUser(5);
        MySqlSessionFactory.releaseSqlSession(session);
    }
}
